package com.suse.ecommerce.uilts.crawler;


import com.google.gson.Gson;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;


/**
 * 爬取商品数据
 */
public class JSoupCrawler {

    public static void main(String args[]) throws IOException {
        run();
    }

    public static Map<String, String> run() throws IOException {

        Map<String, String> resultMap = new HashMap<>();
        String url = "https://tce.alicdn.com/api/data.htm?ids=222887%2C222890%2C222889%2C222886%2C222906%2C222898%2C222907%2C222885%2C222895%2C222878%2C222908%2C222879%2C222893%2C222896%2C222918%2C222917%2C222888%2C222902%2C222880%2C222913%2C222910%2C222882%2C222883%2C222921%2C222899%2C222905%2C222881%2C222911%2C222894%2C222920%2C222914%2C222877%2C222919%2C222915%2C222922%2C222884%2C222912%2C222892%2C222900%2C222923%2C222909%2C222897%2C222891%2C222903%2C222901%2C222904%2C222916%2C222924";
        System.out.println(url);
        String content = Jsoup.connect(url).execute().body();
        System.out.println(content);
        TempMap map = new Gson().fromJson(content, TempMap.class);
        System.out.println("结果：" + map.toString());
        for (Map.Entry<String, Temp> entry : map.entrySet()) {
            System.out.println("key=" + entry.getKey());
            Temp temp = entry.getValue();
            String parentName = temp.getValue().getHead().get(0).getName();
            for (Detail detail : temp.getValue().getList()) {
                String deepName = detail.getName();
                System.out.println(parentName + "   " + deepName);
                resultMap.put(parentName, deepName);
            }
        }
        return resultMap;
    }

}
